package handlers

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

	"chat-app/internal/app/services"
)

type FriendHandler struct {
	friendService services.FriendService
}

func NewFriendHandler(friendService services.FriendService) *FriendHandler {
	return &FriendHandler{friendService: friendService}
}

// SendFriendRequest 发送好友请求
func (h *FriendHandler) SendFriendRequest(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	targetID, err := strconv.ParseUint(c.Param("targetID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid target ID"})
		return
	}

	if err := h.friendService.SendRequest(currentUserID, uint(targetID)); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Friend request sent"})
}

// GetFriendRequests 获取好友请求列表
func (h *FriendHandler) GetFriendRequests(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "20"))

	requests, err := h.friendService.GetPendingRequests(currentUserID, page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get friend requests"})
		return
	}

	c.JSON(http.StatusOK, requests)
}

// RespondToFriendRequest 响应好友请求
func (h *FriendHandler) RespondToFriendRequest(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	requestID, err := strconv.ParseUint(c.Param("requestID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request ID"})
		return
	}

	var req struct {
		Accept bool `json:"accept"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
		return
	}

	if err := h.friendService.RespondToRequest(uint(requestID), currentUserID, req.Accept); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	action := "accepted"
	if !req.Accept {
		action = "rejected"
	}
	c.JSON(http.StatusOK, gin.H{"message": "Friend request " + action})
}

// GetFriendList 获取好友列表
func (h *FriendHandler) GetFriendList(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "50"))

	friends, err := h.friendService.GetFriendList(currentUserID, page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get friend list"})
		return
	}

	c.JSON(http.StatusOK, friends)
}

// RemoveFriend 移除好友
func (h *FriendHandler) RemoveFriend(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	friendID, err := strconv.ParseUint(c.Param("friendID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid friend ID"})
		return
	}

	if err := h.friendService.RemoveFriend(currentUserID, uint(friendID)); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to remove friend"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Friend removed"})
}
